package com.baidu.service.impl;

import com.baidu.utils.ResultResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baidu.pojo.TbUser;
import com.baidu.utils.DuanXinUtils;
import com.baidu.service.TbUserService;
import com.baidu.mapper.TbUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser> implements TbUserService{
    @Autowired
    TbUserMapper tbUserMapper;

    @Autowired
    RedisTemplate redisTemplate;


    @Override
    public void saveTbUser(TbUser tbUser) {
        tbUserMapper.insert(tbUser);
    }


    @Override
    public ResultResponse Login(String userName, String passWord) {

        LambdaQueryWrapper<TbUser> last = new QueryWrapper<TbUser>().lambda().eq(TbUser::getUsername, userName).last("limit 1");

        TbUser tbUser = tbUserMapper.selectOne(last);

        if(tbUser==null){
            return ResultResponse.FAILED(-1,"用户名不存在");
        }

        if(!passWord.equals(tbUser.getPassword())){
            return ResultResponse.FAILED(-2,"密码错误");
        }

        return ResultResponse.SUCCESS("登录成功");
    }
    @Override
    public ResultResponse updatePwd(TbUser tbUser) {
        QueryWrapper<TbUser> username = new QueryWrapper<TbUser>().eq("username", tbUser.getUsername());
        TbUser user = tbUserMapper.selectOne(username);
        if(user==null){
            return ResultResponse.FAILED(-1,"用户名不存在");
        }
        user.setPassword(tbUser.getPassword());
        int i = tbUserMapper.updateById(user);
        if(i>0){
            return ResultResponse.SUCCESS("修改成功");
        }else{
            return ResultResponse.FAILED(-2,"修改失败");
        }
    }

    @Override
    public ResultResponse checkCode(TbUser tbUser) {
        TbUser user=tbUserMapper.findPhone(tbUser.getPhone());
        if(user==null){
            return ResultResponse.FAILED(-1,"手机号不存在");
        }
        //发送验证码
        String code = DuanXinUtils.createCode();
        System.out.println(code);
        //设置过期时间
        return ResultResponse.SUCCESS("发送成功");
    }
}




